/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lone_Wolf
 */
public class BarreMenu extends MenuBar {

    private Menu mnuActions;
    private Menu mnuNouveau;
    private MenuItem mniConsulterStats;
    private MenuItem mniDeconnecter;
    //private MenuItem mniOptions;
    private MenuItem mniNouvClient;
    private MenuItem mniNouvEmploye;
    private MenuItem mniNouvEntree;
    private Fenetre fenetre;

    public BarreMenu(Fenetre fenetre) {
        this.fenetre = fenetre;
        initaliserComposantes();
        initialiserListeners();
        CreerMenuConnexion();
    }

    private void initaliserComposantes() {
        mnuActions = new Menu("Actions");
        mnuNouveau = new Menu("Nouveau");
        mniConsulterStats = new MenuItem("Statistiques Complètes");
        mniDeconnecter = new MenuItem("Se Déconnecter");
        //mniOptions = new MenuItem("Options");
        mniNouvClient = new MenuItem("Nouveau Client");
        mniNouvEmploye = new MenuItem("Nouvel Employé");
        mniNouvEntree = new MenuItem("Nouvelle Entrée");
    }

    private void initialiserListeners() {
        mniNouvClient.addActionListener(new GererEvenements());
        mniNouvEmploye.addActionListener(new GererEvenements());
        mniNouvEntree.addActionListener(new GererEvenements());
        mniDeconnecter.addActionListener(new GererEvenements());
        mniConsulterStats.addActionListener(new GererEvenements());
        //mniOptions.addActionListener(new GererEvenements());
    }

    private void CreerMenuConnexion() {
        removeAll();
        //mnuActions.add(mniOptions);
        mnuActions.add(mniConsulterStats);
        mnuActions.addSeparator();
        mnuActions.add(mniDeconnecter);
        //
        mnuNouveau.add(mniNouvEntree);
        mnuNouveau.add(mniNouvClient);
        mnuNouveau.add(mniNouvEmploye);
        //
        this.add(mnuActions);
        this.add(mnuNouveau);
    }

    public void setEnabled(boolean state) {
        mnuActions.setEnabled(state);
        mnuNouveau.setEnabled(state);
    }

    private void removeAll() {
        while (this.getMenuCount() < 0) {
            this.remove(0);
        }
    }

    class GererEvenements implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == mniNouvEntree) {
                try {
                    fenetre.AfficherEcranNouvEntree();
                } catch (IOException ex) {
                    Logger.getLogger(BarreMenu.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(BarreMenu.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (e.getSource() == mniNouvClient) {
                try {
                    fenetre.AfficherEcranNouvClient();
                } catch (IOException ex) {
                    Logger.getLogger(BarreMenu.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(BarreMenu.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (e.getSource() == mniDeconnecter) {
                try {
                    fenetre.AfficherEcranConnexion();
                } catch (IOException ex) {
                    Logger.getLogger(BarreMenu.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(BarreMenu.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (e.getSource() == mniNouvEmploye) {
                try {
                    fenetre.AfficherEcranNouvEmploye();
                } catch (IOException ex) {
                    Logger.getLogger(BarreMenu.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(BarreMenu.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (e.getSource() == mniConsulterStats) {
                StatsCompletes stats = null;
                fenetre.afficherStatsCompletes(stats);
            }
        }
    }
}
